const jwt=require('jsonwebtoken')

const whiteList=['/login','/register']

const auth=(option,app)=>{
    return async(ctx,next)=>{
        if(whiteList.includes(ctx.path)){
            await next()
        }else{
            let token=ctx.request.header.token
            if(token){
                try{
                    const userinfo=jwt.verify(token,'1906A')
                    await next()
                }catch(err){
                    console.log(err)
                    ctx.body={
                        code:302,
                        msg:'token验证失败'
                    }
                }
            }else{
                ctx.body={
                    code:304,
                    msg:'无token'
                }
            }
        }
    }
}


module.exports=auth